/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package batailledesardennes.test;

import batailledesardennes.util.straightedge.KMultiPolygon;
import batailledesardennes.util.straightedge.KPoint;
import batailledesardennes.util.straightedge.KPolygon;
import batailledesardennes.util.straightedge.PolygonConverter;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;
import java.util.ArrayList;

/**
 *
 * @author charly
 */
public class polymerge {
   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
      KPolygon kPolygon1 = new KPolygon(new KPoint(0, 0), new KPoint(10, 0), new KPoint(10, 10), new KPoint(0, 10));
      KPolygon kPolygon2 = new KPolygon(new KPoint(10, 0), new KPoint(20, 0), new KPoint(20, 10), new KPoint(10, 10));
      KPolygon kPolygon3 = new KPolygon(new KPoint(10, 30), new KPoint(20, 30), new KPoint(20, 40), new KPoint(10, 40));

      PolygonConverter converter = new PolygonConverter();
      Polygon pol1 = converter.makeJTSPolygonFrom(kPolygon1);
      Polygon pol2 = converter.makeJTSPolygonFrom(kPolygon2);
      Polygon pol3 = converter.makeJTSPolygonFrom(kPolygon3);
      Polygon[] array = new Polygon[]{pol1, pol2, pol3};

      GeometryFactory factory = new GeometryFactory();
      GeometryCollection collection = new GeometryCollection(array, factory);
      Geometry merged = collection.buffer(0);
      MultiPolygon multiPol = (MultiPolygon) merged;

      for (int i = 0; i < multiPol.getNumGeometries(); i++) {
         Geometry g = multiPol.getGeometryN(i);
         ArrayList<KMultiPolygon> kmultipol = converter.makeKMultiPolygonListFrom(g);
         System.out.println(kmultipol.toString());
      }
   }
}
